package com.workflowengine.event.support;

import com.workflowengine.config.NodeConfig;
import com.workflowengine.config.WorkFlowConfig;
import com.workflowengine.config.WorkFlowContent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package: com.workflowengine.event
 * @ClassName: WorkflowEventPublisher
 * @Author: h2cloud
 * @CreateTime: 2024/7/15 10:22 下午
 * @Description:
 */
@Component
public class WorkFlowEventPublisher implements ApplicationEventPublisherAware {

	private ApplicationEventPublisher applicationEventPublisher;

	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
		this.applicationEventPublisher = applicationEventPublisher;
	}

	public void publishEvent(ApplicationEvent event) {
		if (event != null) {
			applicationEventPublisher.publishEvent(event);
		}
	}

	public void publishEvent(WorkFlowContent workFlowContent) {
		switch (workFlowContent.getStatus()) {
			case WorkFlowStart :
				publishEvent(workFlowContent.getWorkFlowConfig().getWorkFlowStartEvent());
				break;
			case WorkFlowEnd :
				publishEvent(workFlowContent.getWorkFlowConfig().getWorkFlowEndEvent());
				break;
			case WorkFlowCancel :
				publishEvent(workFlowContent.getWorkFlowConfig().getWorkFlowCancelEvent());
				break;
			case WorkFlowRevoke :
				publishEvent(workFlowContent.getWorkFlowConfig().getWorkFlowRevokeEvent());
				break;
			case NodeStart :
				publishEvent(workFlowContent.getCurNodeConfig().getNodeStartEvent());
				break;
			case NodeEnd :
				publishEvent(workFlowContent.getCurNodeConfig().getNodeEndEvent());
				break;
		}
	}
}

